package com.spontaneappnew.activities;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import com.spontaneappnew.Comment;
import com.spontaneappnew.NavDrawerActivityConfiguration;
import com.spontaneappnew.NavDrawerItem;
import com.spontaneappnew.NavMenuFBHeader;
import com.spontaneappnew.NavMenuItem;
import com.spontaneappnew.Place;
import com.spontaneappnew.Places;
import com.spontaneappnew.R;
import com.spontaneappnew.adapters.AddedPlacesAdapter;
import com.spontaneappnew.adapters.NavDrawerAdapter;
import com.spontaneappnew.adapters.VisitedPlacesAdapter;

public class MyPlacesNew extends AbstractNavDrawerActivity {

	private SectionsPagerAdapter mSectionsPagerAdapter;
	private ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.my_places_pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		final ActionBar actionBar = getActionBar();

	    // Specify that tabs should be displayed in the action bar.
	    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	    // Create a tab listener that is called when the user changes tabs.
	    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
	    	
	        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
	        	 mViewPager.setCurrentItem(tab.getPosition());
	        }
	        
	        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
	            // hide the given tab
	        }
	        
	        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
	            // probably ignore this event
	        }
	    };
	    mViewPager.setOnPageChangeListener(
	            new ViewPager.SimpleOnPageChangeListener() {
	            	
	                @Override
	                public void onPageSelected(int position) {
	                    getActionBar().setSelectedNavigationItem(position);
	                }
	            });
	    actionBar.addTab(actionBar.newTab().setText("Added Places").setTabListener(tabListener));
	    actionBar.addTab(actionBar.newTab().setText("Visited Places").setTabListener(tabListener));
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = new Fragment();
			switch (position) {
			case 0:
				fragment = new AddedPlacesFragment();
				break;
			case 1:
				fragment = new VisitedPlacesFragment();
				break;
			default:
				break;
			} 
			return fragment;
		}

		@Override
		public int getCount() {
			return 2;
		}
	}

	public class VisitedPlacesFragment extends Fragment {

		public VisitedPlacesFragment() {}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_places_visited, container, false);
			ExpandableListView visitedLV = (ExpandableListView) rootView.findViewById(R.id.places_visited_expandableListView);
			ArrayList<Comment> visited = Places.getInstance().getVisitedPlaces();
			visitedLV.setEmptyView(rootView.findViewById(R.id.empty_visited_list_item));
			VisitedPlacesAdapter adap = new VisitedPlacesAdapter(this.getActivity(), visited);
			visitedLV.setAdapter(adap);
			return rootView;
		}
	}
	
	
	public class AddedPlacesFragment extends Fragment {

		public AddedPlacesFragment() {}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_places_added, container, false);
			ExpandableListView addedLV = (ExpandableListView) rootView.findViewById(R.id.places_added_expandableListView);
			ArrayList<Place> added = Places.getInstance().getAddedPlaces();
			addedLV.setEmptyView(rootView.findViewById(R.id.empty_added_list_item));
			AddedPlacesAdapter adap = new AddedPlacesAdapter(this.getActivity(), added);
			addedLV.setAdapter(adap);
			return rootView;
		}
	}

//////////////////////////////////// NAV DRAWER ///////////////////////////////////////////////
	
	@Override
    protected void onNavItemSelected(int id) {
		Intent intent = null;
        switch ((int)id) {
        // Home nav button was pressed
        case 101:
        	intent = new Intent(this, HomeActivity.class);
            break;
        // My places nav drawer was pressed - just close drawer
        case 102:
        	break;
        // Settings nav button was pressed
        case 103:
            intent = new Intent(this, SettingsActivity.class);
            break;
	    }
        if(intent != null)
        	startActivity(intent);
        else
        	super.onNavItemSelected(id);
    }

	@Override
	protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {
		NavDrawerItem[] menu = new NavDrawerItem[] {
        		NavMenuFBHeader.create( 100, "User"),
                NavMenuItem.create(101, "Home", "ic_home", this),
                NavMenuItem.create(102, "My Places", "ic_menu_myplaces", this),
                NavMenuItem.create(103, "Settings", "ic_menu_settings", this),
                NavMenuItem.create(105, "About", "ic_action_about_light", this),
                NavMenuItem.create(107, "Rate Us!", "ic_whats_hot", this),
                NavMenuItem.create(108, "Logout", "ic_menu_logout", this)};
        
        NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
        navDrawerActivityConfiguration.setMainLayout(R.layout.activity_my_places_new);
        navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
        navDrawerActivityConfiguration.setLeftDrawerId(R.id.left_drawer);
        navDrawerActivityConfiguration.setNavItems(menu);
        navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);       
        navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
        navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
        navDrawerActivityConfiguration.setBaseAdapter(
            new NavDrawerAdapter(this, R.layout.navdrawer_item, menu ));
        return navDrawerActivityConfiguration;
	}
}
